package com.rzico.sbl.ui.statistic

import android.view.View
import android.widget.FrameLayout
import android.widget.ImageView
import androidx.core.view.isGone
import androidx.core.view.isInvisible
import androidx.core.view.isVisible
import androidx.recyclerview.widget.GridLayoutManager
import com.rzico.sbl.R
import com.rzico.sbl.databinding.ActivityCommonListBinding
import com.rzico.sbl.model.ManagerContent
import com.rzico.sbl.model.ManagerDivider
import com.rzico.sbl.viewmodel.StatisticStockViewModel
import com.xinnuo.common.extend.condition
import com.xinnuo.common.extend.get
import com.xinnuo.common.extend.startActivityEx
import com.xinnuo.common.helper.dp2px
import com.xinnuo.common_ui.base.BaseActivity
import com.xinnuo.common_ui.base.addItems
import com.xinnuo.common_ui.base.loadGrid
import com.xinnuo.common_ui.view.BadgeTextView
import com.xinnuo.slimadapter.SlimAdapter

class StockActivity : BaseActivity(R.layout.activity_common_list) {

    private val mBinding by lazy { ActivityCommonListBinding.bind(getRootView()) }

    override fun getViewModel() = get<StatisticStockViewModel>()

    override fun initData() {
        initTitle(title = "进销存")

        initLayout()

        mList.addItems(
            getViewModel()
                .stockIcons()
        )
        mAdapter.setDataList(mList)
    }

    private fun initLayout() {
        mBinding
            .listLayout
            .apply {
                recycleList.also { layout ->
                    layout.layoutParams = FrameLayout.LayoutParams(
                        FrameLayout.LayoutParams.MATCH_PARENT,
                        FrameLayout.LayoutParams.MATCH_PARENT
                    ).apply {
                        setMargins(dp2px(15f), 0, dp2px(15f), 0)
                    }

                    layout.loadGrid(
                        refresh = swipeRefresh,
                        onRefresh = {
                            getViewModel()
                                .delay(time = 1000) {
                                    swipeRefresh.isRefreshing = false
                                    mAdapter.notifyDataSetChanged()
                                }
                        },
                        init = {
                            layoutManager = GridLayoutManager(
                                getIContext(),
                                4
                            ).apply {
                                spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
                                    override fun getSpanSize(position: Int): Int = when {
                                        mAdapter.getItem(position) is ManagerContent -> 1
                                        else -> 4
                                    }
                                }
                            }
                        }
                    )

                    mAdapter = SlimAdapter.creator()
                        .register<ManagerDivider>(R.layout.item_manager_divider) { jector, bean, index ->

                            // 解构声明
                            val (title, _) = bean

                            jector.apply {
                                gone(R.id.divder_edit)
                                text(R.id.divder_title, title)

                                getView<View>(R.id.divder_title)
                                    .isGone = index == mList.size - 1

                                getView<View>(R.id.divder_hint1)
                                    .isGone = index == 0

                                getView<View>(R.id.divder_hint2)
                                    .isGone = index == mList.size - 1

                                getView<View>(R.id.divder_hint3)
                                    .isVisible = index == mList.size - 1
                            }
                        }
                        .register<ManagerContent>(R.layout.item_manager_grid) { jector, bean, _ ->

                            // 解构声明
                            val (title, imageId, id, _, _) = bean

                            jector.apply {
                                text(R.id.manager_hint, title)

                                with<ImageView>(R.id.manager_image) {
                                    (imageId > 0)
                                        .condition {
                                            it.setImageResource(imageId)
                                        }
                                }

                                with<BadgeTextView>(R.id.manager_num) {
                                    it.setBadgeCount(bean.count)
                                }

                                getView<View>(R.id.manager_hint).isInvisible = title.isEmpty()
                                getView<View>(R.id.manager_image).isInvisible = title.isEmpty()

                                clicked {
                                    when (id) {
                                        "1" -> startActivityEx<StockGoodsActivity>()
                                        "2" -> startActivityEx<StockBalanceActivity>()
                                        "3" -> startActivityEx<StockCheckActivity>()
                                        "4" -> startActivityEx<StockSupplyActivity>()
                                        "5" -> startActivityEx<StockStoreActivity>()
                                        "6" -> startActivityEx<StockBarrelActivity>()
                                        "7" -> startActivityEx<StockBarrelBalanceActivity>()
                                        "8" -> startActivityEx<StockBarrelCheckActivity>()
                                    }
                                }
                            }
                        }
                        .attachTo(layout)
                }
            }
    }

}